package com.dhcc.DRGsHosp.facade.impl;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.LoginInfoApplication;
import com.dhcc.DRGsHosp.facade.LoginInfoFacade;
import com.dhcc.DRGsHosp.facade.dto.LoginInfoDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.LoginInfoAssembler;



@Named("drgsHospFacadeImpl27")
public class LoginInfoFacadeImpl implements LoginInfoFacade {

	@Autowired
	private LoginInfoApplication loginInfoApplication;

	@Override
	public InvokeResult getLoginInfo(LoginInfoDTO loginInfoDto) {
		String fromDate = loginInfoDto.getFromDate();
		String toDate = loginInfoDto.getToDate();
		
		List<Map<String, Object>> resultMap = new LinkedList<Map<String, Object>>();
		List<Object[]> mList = loginInfoApplication.getLoginInfo(fromDate, toDate);
		for(Object[] obj:mList){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("loginUserCode", obj[0]);
			map.put("loginCount", obj[1]);
			map.put("newLoginTime", obj[2]);
			map.put("newLoginIP", obj[3]);
			map.put("depCode", obj[4]);
			map.put("depName", obj[5]);
			map.put("loginUserName", obj[6]);

			resultMap.add(map);
		}
		return InvokeResult.success(resultMap) ;
	}

	@Override
	public InvokeResult getLastestIssue(LoginInfoDTO loginInfoDTO) {
		String mrDate = loginInfoApplication.getLastestIssue();
		loginInfoDTO.setFromDate(mrDate);
		return InvokeResult.success(loginInfoDTO) ;
	}
	
	public InvokeResult creatLoginInfo(LoginInfoDTO loginInfoDTO) {
		loginInfoApplication.creatLoginInfo(LoginInfoAssembler.toEntity(loginInfoDTO));
		return InvokeResult.success();
	}

	@Override
	public InvokeResult getLoginHistory(String userCode, String dateFrom,
			String dateTo) {
		List<Object[]> list = loginInfoApplication.getLoginHistory(userCode, dateFrom, dateTo);
		List<Map<String, Object>> resultList = new LinkedList<>();
		for(Object[] objs: list){
			Map<String, Object> m = new HashMap<>();
			m.put("loginUserCode", objs[0]);
			m.put("loginUserName", objs[1]);
			m.put("loginIP", objs[2]);
			m.put("loginTime", objs[3]);
			resultList.add(m);
		}
		return InvokeResult.success(resultList);
	}
}
